package com.tank5.tank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyPanel extends JPanel implements KeyListener,Runnable {
    //定义我的坦克
    Hero hero=null;
    //定义敌人坦克,放入vector中
    Vector<EnemyTank> enemyTanks=new Vector<>();
    int enemyTankSise=2;
    //初始化坦克
    public MyPanel(){
        //初始化自己的坦克
        hero=new Hero(400,400);
        hero.setSpeed(5);
        //初始化敌人的坦克
        for(int i=0;i<enemyTankSise;i++){
            EnemyTank enemyTank=new EnemyTank(100*(i+1),0);
            enemyTank.setDirect(2);
            enemyTank.setSpeed(3);
            new Thread(enemyTank).start();
            enemyTanks.add(enemyTank);
            //创建子弹
            Shot shot=new Shot(enemyTank.getX()+20,enemyTank.getY()+70,2);//向下
            //加子弹
            enemyTank.shots.add(shot);
            //启动线程
            new Thread(shot).start();
          //  enemyTanks.add(new EnemyTank(100*(i+1),0));
        }
    }

    //设置画笔
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //游戏界面
        g.fillRect(0, 0, 1000, 750);
        //绘制我的坦克
        if(hero!=null&&hero.isLive()) {
            drawtank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);
        }
        else {
            gameover(g);
            return;
        }
        //绘制我的子弹
//        if (hero.getShot() != null && hero.getShot().isLive()) {
//            g.fill3DRect(hero.getShot().getX(), hero.getShot().getY(), 3, 3, false);
//        }
        //一次发射多个子弹
        for(int i=0;i<hero.shots.size();i++){
            Shot shot=hero.shots.get(i);
            if (hero.getShot() != null && hero.getShot().isLive()) {
                g.fill3DRect(shot.getX(), shot.getY(), 3, 3, false);
            }
            else{//销毁子弹
                hero.shots.remove(shot);
            }
        }

        //绘制敌人的坦克
        if(enemyTankSise==0){
            gamewin(g);
        }
        for (int i = 0; i < enemyTankSise; i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            //判断坦克是否存活
            if (enemyTank.isLive()) {//坦克存活才绘制
                drawtank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);
                //绘制敌人的子弹
                for (int j = 0; j < enemyTank.shots.size(); j++) {
                    //取出子弹
                    Shot shot = enemyTank.shots.get(j);
                    //绘制子弹
                    if (shot.isLive()) {
                        g.fill3DRect(shot.getX(), shot.getY(), 3, 3, false);
                    } else {//子弹已经销毁
                        enemyTank.shots.remove(shot);
                    }
                }
            }
            else{//坦克不存在则删除
                enemyTanks.remove(enemyTank);
                enemyTankSise--;
            }
        }
    }

    public void hitTank(Shot s, Tank tank){
        //判断子弹s击中坦克
        switch(tank.getDirect()){//获取敌人坦克目前方向
            case 0://上
            case 2://下
                if(s.getX()>tank.getX()&&s.getX()<tank.getX()+40&&s.getY()>tank.getY()&&s.getY()<tank.getY()+60){
                    s.setLive(false);//子弹销毁
                    tank.setLive(false);//敌人坦克销毁
                    System.out.println("我打到了");
                }
                break;
            case 1://右
            case 3://左
                if(s.getX()>tank.getX()&&s.getX()<tank.getX()+60&&s.getY()>tank.getY()&&s.getY()<tank.getY()+40){
                    s.setLive(false);//子弹销毁
                    tank.setLive(false);//敌人坦克销毁
                }
                break;
        }
    }


    public void hithero() {
        //判断敌方坦克是否击中我
        //先把敌方坦克取出来
        for (int i = 0; i < enemyTankSise; i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            if (enemyTank.isLive()) {
                for (int j = 0; j < enemyTank.shots.size(); j++) {
                    Shot shot = enemyTank.shots.get(j);
                    if (shot != null && shot.isLive()) {//子弹存活且不为空
                        hitTank(shot, hero);
                    }
                }
            }
        }
    }

    /**
     *
     * @param x  坦克x坐标
     * @param y 坦克y坐标
     * @param g
     * @param direct 坦克方向
     * @param type 坦克类型
     */
//画坦克
    public void drawtank(int x,int y,Graphics g,int direct,int type){
        //根据坦克型号确定画笔颜色
        switch(type){
            case 0:
                //0表示自己的坦克,用蓝色
                g.setColor(Color.green);
                break;
            case 1:
                //1表示敌人的坦克,用黄色
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向绘制坦克主体
        switch(direct){
            case 0:       //0表示坦克向上
                //坦克左轮
                g.fill3DRect(x,y,10,60,false);
                //坦克右轮
                g.fill3DRect(x+30,y,10,60,false);
                //坦克中部
                g.fill3DRect(x+10,y+10,20,40,false);
                //坦克王八盖
                g.fillOval(x+10,y+20,20,20);
                //坦克炮管
                g.drawLine(x+20,y+30,x+20,y-10);
                break;
            case 1:  //1表示向右
                //坦克左轮
                g.fill3DRect(x,y,60,10,false);
                //坦克右轮
                g.fill3DRect(x,y+30,60,10,false);
                //坦克中部
                g.fill3DRect(x+10,y+10,40,20,false);
                //坦克王八盖
                g.fillOval(x+20,y+10,20,20);
                //坦克炮管
                g.drawLine(x+30,y+20,x+70,y+20);
                break;
            case 2://2表示向下
                //坦克左轮
                g.fill3DRect(x,y,10,60,false);
                //坦克右轮
                g.fill3DRect(x+30,y,10,60,false);
                //坦克中部
                g.fill3DRect(x+10,y+10,20,40,false);
                //坦克王八盖
                g.fillOval(x+10,y+20,20,20);
                //坦克炮管
                g.drawLine(x+20,y+30,x+20,y+70);
                break;
            case 3://向左
                //坦克左轮
                g.fill3DRect(x,y,60,10,false);
                //坦克右轮
                g.fill3DRect(x,y+30,60,10,false);
                //坦克中部
                g.fill3DRect(x+10,y+10,40,20,false);
                //坦克王八盖
                g.fillOval(x+20,y+10,20,20);
                //坦克炮管
                g.drawLine(x+30,y+20,x-10,y+20);
                break;
            default:
                break;
        }
    }

    @Override//有字符输入时会触发
    public void keyTyped(KeyEvent e) {

    }

    @Override//键被按下时会触发
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_W){
            //修改坦克方向
            hero.setDirect(0);
            //修改子弹坐标
//            hero.getShot().setDirect(0);
            //修改坦克坐标
            if(hero.getY()>0) {
                hero.moveUp();
            }
        }
        else if (e.getKeyCode()==KeyEvent.VK_D) {
            hero.setDirect(1);
            if(hero.getX()+60<1000) {
                hero.moveRight();
            }
        }
        else if(e.getKeyCode()==KeyEvent.VK_S){
            hero.setDirect(2);
            if(hero.getY()+60<750) {
                hero.moveDown();
            }
        }
        else if(e.getKeyCode()==KeyEvent.VK_A){
            hero.setDirect(3);
            if(hero.getX()>0) {
                hero.moveLeft();
            }
        }
      //  按下J键发射子弹
        if(e.getKeyCode()==KeyEvent.VK_J){
                        hero.shotEnemy();
        }
//            if(hero.getShot()==null||!hero.getShot().isLive())
//            hero.shotEnemy();
//        }
//        //按下J键发射子弹
//        if(e.getKeyCode()==KeyEvent.VK_J) {
//            for (int i = 0; i < hero.shots.size(); i++) {
//                Shot shot = hero.shots.get(i);
//                if (shot == null ) {
//                    hero.shotEnemy();
//                }
//            }
//        }
        this.repaint();
    }

    @Override//键被释放时会被释放
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //判断子弹是否存活
//            if(hero.getShot()!=null&&hero.getShot().isLive()){
//                //进来说明子弹还没销毁.则遍历敌人坦克
//                for(int i=0;i<enemyTankSise;i++){
//                    EnemyTank enemyTank=enemyTanks.get(i);
//                    hitTank(hero.getShot(),enemyTank);
//                }
//            }
            //多个子弹射击敌人
            for(int i=0;i<hero.shots.size();i++){
                Shot shot=hero.shots.get(i);
                if(shot!=null&&shot.isLive()){
                    for(int j=0;j<enemyTankSise;j++){
                        EnemyTank enemyTank=enemyTanks.get(j);
                        hitTank(shot,enemyTank);
                    }
                }
            }
            //敌人射击我
            hithero();
           this.repaint();
        }
    }
    public void gameover(Graphics g){
        //当我被击中时游戏结束
        //绘制结束界面
        g.setColor(Color.white);
        g.fillRect(0,0,1000,750);
        String str="Game over!";
        g.setFont(new Font("楷书",Font.BOLD,80));
        g.setColor(Color.red);
        g.drawString(str,280,350);
    }

    public void gamewin(Graphics g){
        //当我被击中时游戏结束
        //绘制结束界面
        Image image=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/victory.jpg"));
        g.drawImage(image,0,0,1000,750,this);

//        g.setColor(Color.white);
//        g.fillRect(0,0,1000,750);
//        String str="Victory!";
//        g.setFont(new Font("楷书",Font.BOLD,80));
//        g.setColor(Color.green);
//        g.drawString(str,280,350);
    }

}

